import { getUsersList } from '@/api/users'
import { ref } from 'vue'

/**
 * 用户列表服务
 */
export default function () {
  const getUserListLoading = ref(false)
  const userList = ref<Array<UserListItem>>([])
  const getUserListParam = ref<UserAPIRequest.GetUserListParam>({
    levelId: '',
    limit: 50,
    page: 1,
    status: '',
    userContactSearch: '',
    userSearch: '',
    userTypeId: '',
  })
  const total = ref(0)

  const resetGetUserListParam = () => {
    getUserListParam.value = {
      levelId: '',
      limit: 50,
      page: 1,
      status: '',
      userContactSearch: '',
      userSearch: '',
      userTypeId: '',
    }
  }

  const getList = async () => {
    try {
      getUserListLoading.value = true
      const res = await getUsersList(getUserListParam.value)
      getUserListLoading.value = false
      if (res.data.code === 20000) {
        total.value = Number(res.data.data.total)
        userList.value = res.data.data.rows
        return ''
      }

      return res.data.message
    } catch (error) {
      getUserListLoading.value = false
      return '获取用户列表失败，请稍后重试'
    }
  }

  return {
    total,
    userList,
    getUserListParam,
    getUserListLoading,
    getList,
    resetGetUserListParam,
  }
}
